let $;
layui.extend({
    select: '{/}/static/js/formSelects/select'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','select'], function (exports) {
    $ = layui.$;
    let form = layui.form;
    let select = layui.select;
    zmnLayUtil.layFormVerify(form);

    /**
     * 描述: 初始化页面
     **/
    var init = function (){
        $("#storeContainer").html($("#storeItem").html());
        form.render("select");
    }();

    /**
     * 描述: 添加
     **/
    form.on('submit(add)',function (obj) {
        var $parent = $(this).parent().parent().parent();
        var $this = $(this);
        $this.attr("disabled","disabled");
        var city = json.city;
        var field = obj.field;
        for (var i = 0;i<city.length;i++){
            if (city[i].value == field.cityId){
                field.cityName = city[i].text;
                break;
            }
        }
        $.ajax({
            type: "POST",
            url: "/coup/review/addStore.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(field),
            success: function (json) {
                if (json.status == 200) {
                    $parent.find($("input[name^='id']")).val(json.id);
                    $parent.find($("input")).attr("disabled","disabled");
                    $parent.find($("select")).attr("disabled","disabled");
                    //批量新增店铺时，防止后面的店铺选择公司名称影响上一个店铺的商家
                    $parent.find($("select[id^='merId']")).attr("id","");
                    $parent.parent().append($("#storeItem").html());
                    $this.removeAttr("lay-filter");
                    $this.removeAttr("lay-submit");
                    $this.addClass("storeDelete");
                    $this.children().removeClass("layui-icon-add-1").addClass("layui-icon-delete")
                    form.render("select");
                }
                $this.removeAttr("disabled");
                layer.msg(json.message);
            }
        });
        return false;
    });

    /**
     * 描述: 删除
     **/
    $(document).on("click",".storeDelete",function () {
        var $this = $(this);
        var $parent = $(this).parent().parent().parent();

        $this.attr("disabled","disabled");

        var relateIds = [$parent.find($("input[name^='id']")).val()];

        $.ajax({
            type: "POST",
            url: "/coup/review/delCoupStore.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify({relateIds:relateIds}),
            success: function (json) {
                if (json.status == 200) {
                    $parent.remove();
                    $this.attr("disabled","disabled");
                }
                layer.msg(json.message);
            }
        });
        return false;
    })

    form.on('submit(zmn-review-modify-form)', function (obj) {
        $(this).attr("disabled","disabled");
        var city = json.city;
        var field = obj.field;
        for (var i = 0;i<city.length;i++){
            if (city[i].value == field.cityId){
                field.cityName = city[i].text;
                break;
            }
        }
        $.ajax({
            type: "POST",
            url: "/coup/review/addStore.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(field),
            success: function (json) {
                if (json.status == 200) {
                    zmnLayUtil.closeThisIframeMsg(parent, undefined, "zmn-review-table");
                }
                layer.msg(json.message);
            }
        });
        $(this).removeAttr("disabled");
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        zmnLayUtil.closeThisIframeMsg(parent, undefined, "zmn-review-table");
    });

    // 公司商家下拉联动
    var selectOption = {
        elem: '#companyCode',
        child: {
            url: '/coup/review/getCoupStoresByCompanyCode.action',
            param: 'companyCode',
            elem: '#merId',
            // child：{}
        }
    };
    select.render(selectOption);
    //对外暴露的接口
    exports('review_add', {});
});
